<script setup lang="ts">
import { effect, onMounted, reactive } from 'vue';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
let IntervalId:number;
const methods = {
	Attack: async () => {
		state.isAttacking = !state.isAttacking;
		if (!state.isAttacking) {
			state.buttonText = '开始攻击';
			state.buttonStyle.backgroundColor = '#409eff';
			const { data } = await axios.post('/ipv6api/stop');
			state.polling = true;
			clearInterval(IntervalId);
		} else {
			state.buttonText = '停止攻击';
			state.buttonStyle.backgroundColor = 'red';
			const { data } = await axios.post('/ipv6api/start?victim=' + state.form.victim + '&mode=' + state.form.mode + '&domain=' + state.form.domain + '&ip=' + state.form.ip + '&ttl=' + state.form.ttl
			);
			state.polling = false;
		}

		const fetchUpdate = async () => {
			const response = await axios.get('/ipv6api/history');
			state.attackline = response.data.join('');
		}

		if (!state.polling) {
			IntervalId = setInterval(fetchUpdate, 1000);
		} else {
			const stopPolling = () => {
				clearInterval(IntervalId);
			};
		}
	},
	GetEffect: async () => {
		const { data } = await axios.get('/ipv6api/getresult?domain='+state.form.domain);
		state.effect = data
	},
	GetStatus: async () => {
		const { data } = await axios.get('/ipv6api/status');
		state.effect = data
	}
};

onMounted(async () => {
	const { data } = await axios.get('/ipv6api/status');
	console.log(data);

	if(data == 2 || data == 0 || data == 3){
		//呵呵
		
	}else{
		//呵呵
		state.buttonText = '停止攻击';
		state.buttonStyle.backgroundColor = 'red';
		state.isAttacking = true;
	}
});

interface UserForm {
	victim: string;
	mode: string;
	domain: string;
	ip: string;
	ttl: string;
}

const state = reactive({
	form: {
		victim: '2001:250:200:7:d703:82e0:a74b:3c88',
		domain: 'www.stanford.edu',
		ip: '202.112.51.126',
		ttl: '86400',
		mode: '0',
	} as UserForm,
    buttonText: '开始攻击',
	buttonStyle: { backgroundColor: ''},
	isAttacking: false,
	effect: '',
	attackline : "",
	polling: false
});
</script>

<template>
	<div>
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/' }" class="bread">主页</el-breadcrumb-item>
			<el-breadcrumb-item :to="{ path: '/IPv6' }" class="bread">IPv6</el-breadcrumb-item>
			<el-breadcrumb-item class="bread">IPv6注入和篡改</el-breadcrumb-item>
		</el-breadcrumb>
	</div>

	<div style="display: flex;">

		<div style="width: 70%;">

			<div class="display-flex j-c-c a-i-c height100">
				<div class="login-form">
					<h1 class="title">请输入IPv6地址和模式</h1>
					<el-form :model="state.form">

						<div>
							<el-form :model="state.form" label-width="100px">
								<el-form-item prop="email" label="攻击IPv6地址">
									<el-input v-model="state.form.victim" placeholder="请输入IPv6地址" disabled></el-input>
								</el-form-item>
								<el-form-item prop="email" label="目标域名">
									<el-input v-model="state.form.domain" placeholder="请输入域名"></el-input>
								</el-form-item>
								<el-form-item prop="email" label="注入/篡改IP">
									<el-input v-model="state.form.ip" placeholder="请输入IP"></el-input>
								</el-form-item>
								<el-form-item prop="email" label="注入/篡改TTL">
									<el-input v-model="state.form.ttl" placeholder="请输入TTL"></el-input>
								</el-form-item>


							</el-form>
						</div>

						<el-radio-group v-model="state.form.mode">
							<el-radio value="0" v-model="state.form.mode">篡改模式</el-radio>
							<el-radio value="1" v-model="state.form.mode">注入模式</el-radio>
						</el-radio-group>

						<el-form-item class="button-container">
							<el-button class="Mybutton" type="primary" @click="methods.Attack"
								:style="{ backgroundColor: state.buttonStyle.backgroundColor }" target="true">
								{{ state.buttonText }}
							</el-button>
							<el-button class="Mybutton" type="primary" @click="methods.GetEffect">
								更新域名解析效果
							</el-button>
						</el-form-item>
					</el-form>
				</div>
			</div>

			<el-scrollbar class="Output">
				<el-form :inline="true" class="demo-form-inline">
					<el-form-item>
						<el-input v-model="state.attackline" placeholder="攻击输出" clearable :rows="15"
							:autosize="{ minRows: 15, maxRows: 15 }" readonly="true" class="OutputBox" type="textarea"
							autoc />
					</el-form-item>
					<el-form-item>
						<el-input v-model="state.effect" placeholder="效果输出" clearable :rows="15"
							:autosize="{ minRows: 15, maxRows: 15 }" readonly="true" class="OutputBox"
							type="textarea" />
					</el-form-item>
				</el-form>
			</el-scrollbar>
		</div>
		<div style="width: 20%;">
			<img src="@/assets/IPV6INSERT.jpg" alt="tsinghua" style="width: 150%; height: auto;">
		</div>
	</div>

	<div style="margin-top: 20px; text-align: center;">

	</div>
	<div style="display: flex; justify-content: center; margin-top: 20px; width: 70%;">
		<div>
			<h2>IPv6 篡改验证视频</h2> <!-- Added title -->
			<div style="margin: 0 10px;">
				<video width="600" controls>
					<source src="@/assets/IPv6篡改-剪辑后.mp4" type="video/mp4">
					Your browser does not support the video tag.
				</video>
			</div>
		</div>
		<div>
			<h2>IPv6 注入验证视频</h2> <!-- Added title -->
			<div style="margin: 0 10px;">
				<video width="600" controls>
					<source src="@/assets/IPv6注入-剪辑后.mp4" type="video/mp4">
					Your browser does not support the video tag.
				</video>
			</div>
		</div>

	</div>
</template>


<style lang="css" scoped>
.login-form {
	width: 400px;
	height: 370px;
	margin: 0 auto;
	padding: 30px;
	border-radius: 2px;
	margin-top: 10px;
	box-shadow: 3px 3px 10px 8px #f1f1f1;
}

.OutputBox {
	font-family: Cascadia;
	width: 545px;
	height: 320px;
	margin: 0 auto;
	padding: 10px;
	border-radius: 2px;
	box-shadow: 3px 3px 10px 8px #f1f1f1;
}

.code-box {
	position: absolute;
	right: 0;
	top: 0;
	cursor: pointer;
}

.Output {
	position: relative;
	margin-top: 10px;
}

.bread {
    font-size: 34px;
    color: rgb(67, 166, 166);
}

.Mybutton {
	text-align: center;
	margin: 0 auto;
	
}

.button-container {
  margin-top: 20px; /* 增加按钮与上方的距离 */
  text-align: center; /* 使按钮居中对齐 */
}

</style>